package com.wlz;

import com.wlz.servlet.BatServlet1;
import com.wlz.servlet.BatServlet2;
import com.wlz.servlet.HttpServlet;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 客户端连接后，保持连接，通过第一次有事件发生的时候把对应的ChannelHandlerContext留存，
 * 通过ChannelHandlerContext进行给客户端的消息推送
 * 源码学院-ant
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
public class NettyServer {
    static ConcurrentHashMap<String, HttpServlet> servletMap=new ConcurrentHashMap<>();
    static {
        servletMap.put("/bat1",new BatServlet1());
        servletMap.put("/bat2",new BatServlet2());
    }

//    private static final boolean SSL = false;

    public void bind(int port) throws Exception {
//        final SslContext sslCtx;
//        if (SSL) {
//            //netty为我们提供的ssl加密，缺省
//            SelfSignedCertificate ssc = new SelfSignedCertificate();
//            sslCtx = SslContextBuilder.forServer(ssc.certificate(),
//                    ssc.privateKey()).build();
//        }

        NettyServerHandler serverHandler = new NettyServerHandler();

        EventLoopGroup bossGroup = new NioEventLoopGroup();//selector[]
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();//服务端引导程序
            b.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //https协议
//                            if (sslCtx != null) {
//                                socketChannel.pipeline().addLast(sslCtx.newHandler(socketChannel.alloc()));
//                            }
                            socketChannel.pipeline().addLast(new HttpServerCodec());
                            socketChannel.pipeline().addLast(new HttpObjectAggregator(65536));
                            //启用http压缩  nginx
//                            socketChannel.pipeline().addLast("compressor",new HttpContentCompressor());
                            socketChannel.pipeline().addLast(serverHandler);
                        }
                    });
            ChannelFuture f = b.bind(port).sync();
            System.out.println("server start");
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }

    }

    public static void main(String[] args) throws Exception {
        int port = 8080;
        new NettyServer().bind(port);
    }
}
